// __PUBLIC__/js/page/admin/push/apppush
require(['../../common'], function() {
  require(['push.apppush'])
})

define('push.apppush', [
  'jquery',
  'css!ke/themes/default/default.css',
  'ke/kindeditor',
  'ke/lang/zh_CN',
  'jqp/validate'
], function($, Grid) {
  'use strict';

  $('#apppush_form').validate({
    classPlace: '.kv',
    errorPlace: '.kv-error',
    errors: {
      content: {
        required: '请填写推送内容',
      },
    }
  })

  function getChecked() {
    var ids = new Array();
    $.each($("input:checkbox[name='checkbox']:checked"), function(i, n) {
      ids.push($(n).val());
    });
    return ids;
  }
  $(document).ready(function() {
    $('#select_user').click(function() {
    	var url = APP + '?app=admin&m=User&a=userdialog'  
        var dialog = KindEditor.dialog({
          width: 800,
          height: 550,
          title: '选择用户',
          body: '<iframe id="user-frame" name="userFrame" style="width:100%;height:100%" frameborder="0" src="' + url + '"></iframe>',
          closeBtn: {
            name: '关闭',
            click: function(e) {
              dialog.remove();
            }
          },
          yesBtn: {
            name: '确定',
            click: function(e) {
              var ids = new Array()
  	          $('#user-frame').contents().find('tbody').find(':checked').each(function() {
  	        	ids.push($(this).closest('tr').data('value'))
  	          })
              var oldval = new Array()
              var i = 0
              $("input[name^='select_users']").each(
                function() {
                  oldval[i] = $(this).val()
                  i++
                }
              )
              var html = ""
              for (var i = 0; i < ids.length; i++) {
                var val = ids[i].split("_")
                if ($.isArray(oldval) && oldval.length > 0) {
                  if ($.inArray(val[0], oldval) > -1) {
                    continue
                  }
                  html += '<p>'
                  html += '<input type="hidden" name="select_users[]" value="' + val[0] + '"/>'
                  html += '<a href="javascript:;" class="A_ico_top">' + val[1] + '</a>&nbsp;&nbsp; '
                  html += '</p>'
                } else {
                  html += '<p>'
                  html += '<input type="hidden" name="select_users[]" value="' + val[0] + '"/>'
                  html += '<a href="javascript:;" class="A_ico_top">' + val[1] + '</a>&nbsp;&nbsp; '
                  html += '</p>'
                }
              }
              $("#select_userlist_div").append(html)
              dialog.remove()
            }
          },
          noBtn: {
            name: '取消',
            click: function(e) {
              dialog.remove()
            }
          }
        });
    });
  });

  $(document).on('change', '#roleid', function(e) {
    e.preventDefault()
    var id = $("#roleid").val()
    if (id == -1) {
      $("#select_user").show()
    } else {
      $("#select_user").hide()
      $("#select_userlist_div").html('')
    }
  })
    .on('click', '.A_ico_top', function(e) {
      e.preventDefault()
      $(this).parent().remove()
    })
    .on('click', '#J_do', function(e) {
    	apppush()
    })

  $('#J_search').on('click', function(e) {
    e.preventDefault()
    searchData()
  })
  $('.toggle-all').on('click', function(e) {
    e.preventDefault()
    alert(' check all ')
    checkAll(this)
  })
  

  function searchData() {
    var vuid = $("#dialog_uid").val()
    var vuname = $("#dialog_uname").val()
    var vemail = $("#dialog_email").val()
    var par_get = '&uid=' + vuid + '&uname=' + vuname + '&email=' + vemail
    getUserList(APP + '?app=admin&m=User&a=userdialog' + par_get, vuid, vuname, vemail)
  }

  function getUserList(url, uid, uname, email) {
    $.get(url, function(res) {
      $("#dialoghtml").html(res)
      $("#dialog_uid").val(uid)
      $("#dialog_uname").val(uname)
      $("#dialog_email").val(email)
    })
  }

  function apppush() {
    var id = $('#roleid').val()
    var content = $('#content').val()
    var userlist = new Array()
    if (id == -1) {
      var i = 0
      $("input[name^='select_users']").each(
        function() {
          userlist[i] = $(this).val()
          i++
        }
      )
      if (userlist.length < 1) {
        alert('请选择用户')
        return false
      }
    }
    $.post(APP + '?app=admin&m=Push&a=doapppush', {
      id: id,
      content: content,
      userlist: userlist
    }, function(res) {
      alert(res)
    })
  }
})
